package com.soft.line.controller;


import com.soft.line.entity.CertificateInfo;
import com.soft.line.service.CertificateInfoService;
import com.soft.line.service.CodeDetailService;
import com.soft.line.util.exception.CommonResult;
import com.soft.line.util.exception.ResultData;
import com.soft.line.util.jackson.converter.StringToDateUtil;
import com.soft.line.util.poi.PoiUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author wu.xueqiao on 2020/5/8.
 * @version 0.1
 */

@Api(description = "证书接口")
@Slf4j
@RestController
@RequestMapping("/dataManage")
public class CertificateInfoController {

    @Autowired
    private CertificateInfoService certificateInfoService;
    @Autowired
    private CodeDetailService codeDetailService;


    /**
     * @param map 参数：pageSize，pageNum，查询条件：证书名称，人员姓名等
     * @return
     * @Description 证书查询 模糊查询 个人证书查询 分页
     */
    @ApiOperation(value = "证书列表", notes = "查询证书列表")
    //设置请求参数
    @ApiImplicitParams({
            // @ApiImplicitParam(paramType = "query", name = "userName", value = "名称", required = false, dataType = "String"),
            //  @ApiImplicitParam(paramType = "query", name = "dicCode", value = "编码", required = false, dataType = "String")
            //    .....
    })
    // @Permission
    @PostMapping("/getCompanyAllCertificates")
    public CommonResult getCompanyAllCertificates(@RequestBody Map<String, Object> map) {
        return CommonResult.success(certificateInfoService.getAllCompanyCertificates(map));
    }

    @ApiOperation(value = "获取所有证书列表", notes = "获取所有证书列表")
    @PostMapping("/certificates")
    public CommonResult getAllCertificates(@RequestBody Map<String, Object> map) {
        return CommonResult.success(certificateInfoService.getAllCertificates(map));
    }

    @ApiOperation(value = "删除证书", notes = "删除证书")
    @DeleteMapping("/certificate")
    public CommonResult deleteCertificate(@RequestBody Map<String, Object> map) {
        certificateInfoService.delete(map);
        return CommonResult.success("删除成功");
    }

    @ApiOperation(value = "保存证书", notes = "保存证书")
    @PostMapping("/certificate")
    public CommonResult saveCertificate(@RequestBody @Validated CertificateInfo certificateInfo) {
        if (certificateInfoService.countByCId(certificateInfo.getId(), certificateInfo.getCertId()) > 0) {
            return CommonResult.success("存在相同证书编号","存在相同证书编号");
        }
        if (certificateInfo.getBeginDate() != null && certificateInfo.getEndDate() != null) {
            if (certificateInfo.getBeginDate().after(certificateInfo.getEndDate())) {
                return CommonResult.success("起始日期不能>终止日期", "起始日期不能>终止日期");
            }
        }
        if (certificateInfo.getId() == null) {
            //添加
            certificateInfoService.add(certificateInfo);
            return CommonResult.success("添加成功");
        } else {
            //修改
            certificateInfoService.update(certificateInfo);
            return CommonResult.success("修改成功");
        }
    }

    @ApiOperation(value = "导入证书", notes = "导入证书")
    @PostMapping("/certificate/import")
    public CommonResult importCertificate(@RequestParam MultipartFile file, HttpServletRequest request) {
        System.out.println("importCertificate");
        if (file == null){
                return CommonResult.success("上传文件不能为空","上传文件不能为空");
        }
            String fileName = file.getOriginalFilename();
            if (!fileName.matches("^.+\\.(?i)(xls)$") && !fileName.matches("^.+\\.(?i)(xlsx)$")) {
                return CommonResult.success("上传文件格式错误，请上传后缀为.xls或.xlsx的文件","上传文件格式错误，请上传后缀为.xls或.xlsx的文件");
            }
            Map<String,Object> map = PoiUtils.excel2Certificate(file,codeDetailService,certificateInfoService);
            String warningMsg = (String)map.get("warningMsg");
            if (warningMsg.length()>0){
                return CommonResult.failed(warningMsg);
            }else{
                List<CertificateInfo> certificateInfoList = (List<CertificateInfo>)map.get("certificateInfoList");
                certificateInfoService.insertRecord(certificateInfoList);
                return CommonResult.success("导入成功");
            }

    }

    @ApiOperation(value = "导出证书", notes = "导出证书")
    @PostMapping("/certificate/export")
    public void exportCertificate(@RequestBody Map<String, Object> map,HttpServletResponse response) {
        List<CertificateInfo> certificateInfoList = certificateInfoService.getAllByIDList(map);
        PoiUtils.certificate2Excel(certificateInfoList,response);
    }

}
